package com.sin.j2me;


import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

import com.sin.j2me.framework.Debug;

public class SoundPlayer implements Runnable {
	private String filename = null;
	private Player player = null;
	public SoundPlayer(String filename) {
		this.filename = filename;
	}
	
	public void start() {
		Thread thread = new Thread(this);
		thread.start();
	}
	
	public void stop() {
		if(this.player != null){
			try {
				this.player.stop();
			} catch (MediaException e) {
				Debug.Error(e);
			}
			finally {
				this.player = null;
			}
		}
	}

	public void run() {
		System.out.println("start play:" + this.filename);
		try {
			player = Manager.createPlayer(filename);
			player.start();
		} catch (Exception e) {
			Debug.Error(e);
			try {
				player.stop();
			} catch (MediaException e1) {
				Debug.Error(e1);
			}
			finally {
				this.player = null;
			}
		}
	}
}
